<?php

namespace App\Repositories;

use App\Models\Users;

class UsersRepository
{
    //详情
    public function info($param)
    {
        return Users::where($param)->first();
    }

    // 列表
    public function list($where = [])
    {
        return Users::where($where)
            ->get();
    }
    // 列表
    public function listIn($aid)
    {
        return Users::whereIn('id',$aid)
            ->get();
    }
    // 分页列表
    public function pageList($where = [])
    {
        !isset($where['page']) ? $page = 15 : $page = $where['page'];

        return $userList = Users::orderBy('created_at', 'desc')
            ->where(function ($query) use ($where) {

                if (isset($where['nickname'])) {
                    $query->where('nickname', 'like', '%' . $where['nickname'] . '%');
                }

                if (isset($where['mobile'])) {
                    $query->where('mobile', 'like', '%' . $where['mobile'] . '%');
                }

                if (isset($where['start_time']) && isset($where['end_time'])) {
                    $query->whereBetween('created_at', [$where['start_time'],$where['end_time']]);      
                }

            })->paginate($page);
    }

    //新增
    public function create($param)
    {
        $Users = new Users();
        $Users->fill($param)->save();
        return $Users;
    }

    // update
    public function update($param)
    {
        $sys = Users::find($param['id']);
        return $sys->fill($param)->save();
    }
    // del
    public function delete($id)
    {
        return Users::where('id', $id)->delete();
    }

    public function infoBytoken($token)
    {
        return Users::select('id', 'avatar', 'token_time', 'nickname')->where('login_token', $token)->first();
    }
     //用户总金额
    public function countYe($param=[])
    {
        return Users::sum('balance');
    }
    public function countUser($param=[])
    {
        return Users::count();
    }
}
